package com.theyeasy.dvmini.session;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.google.common.base.Strings;

public class PCSessionHelper implements Serializable{
	public enum PCSessionName  
	{ 
		Account
	}

	public static void SetSession(PCSessionName name, Object value) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		request.getSession().setAttribute(name.name(), value);
	}

	public static String GetSession(PCSessionName name) {
		try {
			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
					.getRequest();
			Object val = request.getSession().getAttribute(name.name());
			if (val == null)
				return "";
			return val.toString();
		} catch (Exception e) {
			return "";
		}
	}

	public static Object GetSession(String name) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();

		return request.getSession().getAttribute(name);
	}
	 
	/**
	 * 清除平台后台session
	 */
	public static void removeBrowser() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		Class<PCSessionName> clz = PCSessionName.class;

		// request.getSession().removeAttribute(SessionHelper.SessionName.Code.name());
		// 清除所有
		for (Object obj : clz.getEnumConstants()) {
			request.getSession().removeAttribute(obj.toString());
		}
	}
	
	/**
     * 获取登录用户 账号
     * 
     * @return
     */
    public static String getUserAccount() {
        String account = GetSession(PCSessionName.Account);
        return (Strings.isNullOrEmpty(account) ? null : account);
    }
	 
}